package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Question UserController
 * @Date 2023/1/4 15:24
 * @Solution
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/reg")
    public void reg(HttpServletRequest request, HttpServletResponse response,String username,String password) throws IOException {
        userService.add(username,password);
        response.sendRedirect("/login.html");
    }
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response,String username,String password) throws IOException {
        User user = userService.getByName(username,password);
        if(user==null) {
            return "不存在该用户";
        }
        if(user.getPassword().equals(password)) {
            HttpSession session = request.getSession();
            session.setAttribute("userinfo",user.getUsername());
            response.sendRedirect("menu");
        }
        return "未输入完整信息";
    }
    @RequestMapping("/menu")
    public void menu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect("/myblog_list.html");
    }
}
